var express = require('express');
var router = express.Router();
var { shopModel, typeModel, brandModel, labelModel } = require('../../../model/model');
var multiparty = require('multiparty')

/* GET home page. */
router.post('/getGoods', async (req, res, next) => {
  let list = await shopModel.find().populate('type');
  res.send({
    conde: 200,
    list
  });
});

//删除商品
router.post('/deleteGoods', async (req, res, next) => {
  let { id } = req.body;
  await shopModel.deleteOne({ _id: id });
  res.send({
    code: 200,
    msg: '删除成功'
  })
})


//修改商品
router.post('/updateGoods', async (req, res, next) => {
  let { _id, name, specification, price, discount, limited, label, brand } = req.body.data;
  await shopModel.updateOne({ _id: _id }, { name, specification, price, discount, limited, label, brand });
  res.send({
    code: 200,
    msg: '修改成功'
  })
})

//修改商品状态
router.post('/updateGoodsStatue', async (req, res, next) => {
  let { id, state } = req.body.data;
  console.log(id, state);
  await shopModel.updateOne({ _id: id }, { statue: state });
  res.send({
    code: 200,
    msg: '修改成功'
  })
})

//获取到商品类型
router.post('/getGoodsType', async (req, res, next) => {
  let list = await typeModel.find();
  console.log(list);
  res.send({
    code: 200,
    list
  })
})

//搜索数据
router.post('/searchGoods', async (req, res, next) => {
  let { name, inventory, label, statue } = req.body.data.goods_look;
  let list_look = []
  console.log(name, inventory, label, statue);
  if (name) list_look.push({ name: { $regex: name } })
  if (inventory) {
    let min = inventory.split('-')[0]
    let max = inventory.split('-')[1]
    list_look.push({ inventory: { $gte: min, $lte: max } })
  }
  if (label) list_look.push({ label: { $regex: label } })
  if (statue) list_look.push({ statue: { $regex: statue } })
  let list = await shopModel.find({ $and: list_look });
  console.log(list)
  res.send({
    code: 200,
    list
  })
})

// 图片上传
router.post('/upload', async (req, res) => {
  let form = new multiparty.Form()
  form.uploadDir = 'upload'
  form.parse(req, (err, a, b) => {
    let path = "http://localhost:3000/" + b.file[0].path;
    res.send({
      code: 200,
      img: path
    })
  })
})

//获取商品类型
router.post('/getGoodsTypeList', async (req, res, next) => {
  let list = await typeModel.find();
  res.send({
    code: 200,
    list
  })
})
//获取到商品品牌
router.post('/getGoodsBrandList', async (req, res, next) => {
  let list = await brandModel.find();
  res.send({
    code: 200,
    list
  })
})
//获取商品标签
router.post('/getGoodsTagList', async (req, res, next) => {
  let list = await labelModel.find();
  res.send({
    code: 200,
    list
  })
})

//添加商品
router.post('/addGoods', async (req, res, next) => {
  let { data } = req.body.data;
  await shopModel.create(data);
  res.send({
    code: 200,
  })
})

//修改商品类别
router.post('/addGoodsType', async (req, res, next) => {
  let { _id, type, commission, item_type } = req.body.data;
  console.log(_id, type, commission, item_type);
  await typeModel.updateOne({ _id: _id }, { type, commission, item_type });
  res.send({
    code: 200,
  })
})

//删除数据
router.post('/del_type', async (req, res, next) => {
  let { _id } = req.body.data;
  console.log(_id);
  await typeModel.deleteOne({ _id: _id });
  res.send({
    code: 200,
  })
})

//商品类型列表添加
router.post('/addGoodsList', async (req, res, next) => {
  let { data } = req.body.data;
  await typeModel.create(data);
  res.send({
    code: 200,
  })
})
//商品类型搜索
router.post('/searchGoodsList', async (req, res, next) => {
  let { goods_type_look } = req.body.data;
  console.log(goods_type_look);
  let list = await typeModel.find({ type: { $regex: goods_type_look } });
  res.send({
    code: 200,
    list
  })
})

//商品品牌修改
router.post('/editGoodsBrand', async (req, res, next) => {
  let { _id, name, Inglist_name, statue } = req.body.data;
  let list = await brandModel.updateOne({ _id: _id }, { name, Inglist_name, statue });
  res.send({
    code: 200,
    list
  })
})

//删除
router.post('/delGoodsBrand', async (req, res, next) => {
  let { _id } = req.body.data;
  await brandModel.deleteOne({ _id: _id });
  res.send({
    code: 200,
  })
})

//添加
router.post('/addGoodsBrand', async (req, res, next) => {
  let { data } = req.body.data;
  await brandModel.create(data);
  res.send({
    code: 200,
  })
})

//商品品牌搜索
router.post('/searchGoodsBrand', async (req, res, next) => {
  let { goods_brand_look } = req.body.data;
  let list = await brandModel.find({ name: { $regex: goods_brand_look } });
  res.send({
    code: 200,
    list
  })
})

//商品标签修改
router.post('/editGoodsTag', async (req, res, next) => {
  let { _id, name, Text, statue } = req.body.data;
  let list = await labelModel.updateOne({ _id: _id }, { name, Text, statue });
  res.send({
    code: 200,
    list
  })
})

//删除
router.post('/delGoodsTag',async(req,res)=>{
  let {_id}=req.body.data
  await labelModel.deleteOne({_id:_id})
  res.send({
    code:200,
  })
})

//添加
router.post('/addGoodsTag', async (req, res, next) => {
  let { data } = req.body.data;
  await labelModel.create(data);
  res.send({
    code: 200,
  })
})

//商品标签搜索
router.post('/searchGoodsTag', async (req, res, next) => {
  let { goods_label_look } = req.body.data; 
  let list=await labelModel.find({ name: { $regex: goods_label_look } });
  console.log(goods_label_look);
  res.send({
    code: 200,
    list
  })
})
module.exports = router